﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfLibrary1_CustomControlLib
{
    /// <summary>
    /// ColorPicker.xaml 的交互逻辑
    /// </summary>
    public partial class ColorPicker : UserControl
    {
        #region 01 - 属性和依赖项属性
        public Color Color
        {
            get => (Color)GetValue(ColorProperty); set { SetValue(ColorProperty, value); }
        }

        public byte Red { get => (byte)GetValue(RedProperty); set { SetValue(RedProperty, value); } }
        public byte Green { get => (byte)GetValue(GreenProperty); set { SetValue(GreenProperty, value); } }
        public byte Blue { get => (byte)GetValue(BlueProperty); set { SetValue(BlueProperty, value); } }

        public static readonly DependencyProperty ColorProperty;
        public static readonly DependencyProperty RedProperty;
        public static readonly DependencyProperty GreenProperty;
        public static readonly DependencyProperty BlueProperty;
        #endregion

        public event RoutedPropertyChangedEventHandler<Color> ColorChanged
        {
            add { AddHandler(ColorChangedEvent, value); }
            remove { RemoveHandler(ColorChangedEvent, value); }
        }
        public static readonly RoutedEvent ColorChangedEvent;

        public ColorPicker()
        {
            InitializeComponent();
        }

        static ColorPicker()
        {
            #region 02-注册依赖项属性
            ColorProperty = DependencyProperty.Register("Color",
                typeof(Color),
                typeof(ColorPicker),
                new(OnColorChanged) { DefaultValue = Colors.Black });//Color是需要一个初始值

            RedProperty = DependencyProperty.Register("Red",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));

            GreenProperty = DependencyProperty.Register("Green",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));

            BlueProperty = DependencyProperty.Register("Blue",
                typeof(byte),
                typeof(ColorPicker),
                new(OnRGBChanged));
            #endregion

            //注册路由事件
            ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble,
                typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPicker));
        }
        private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //获得调用方的实例和
            ColorPicker colorPicker = (ColorPicker)sender;

            Color newColor = (Color)e.NewValue;
            Color oldColor = (Color)e.OldValue;

            //同样需要解析Color到RGB
            colorPicker.Red = newColor.R;
            colorPicker.Green = newColor.G;
            colorPicker.Blue = newColor.B;

            //要在颜色改变之后，调用事件
            RoutedPropertyChangedEventArgs<Color> args = new (oldColor, newColor) { RoutedEvent = ColorChangedEvent };
            colorPicker.RaiseEvent(args);


        }

        private static void OnRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //获得调用方的实例和字段
            ColorPicker colorpicker = (ColorPicker)sender;
            Color color = colorpicker.Color;

            if (e.Property == RedProperty)
            {
                color.R = (byte)e.NewValue;
            }
            else if (e.Property == GreenProperty)
            {
                color.G = (byte)e.NewValue;
            }
            else if (e.Property == BlueProperty)
            {
                color.B = (byte)e.NewValue;
            }

            colorpicker.Color = color;
        }

  
    }
}
